package cc.wsyw126.java.collection.set;

import cc.wsyw126.java.model.User;

import java.util.Date;
import java.util.HashSet;

/**
 * Created by junpeng.yang on 16/8/25.
 */
public class CanOrNotModifySetElements {
    public void testModifyUser() {
        System.out.println("--------------------------------------------");
        HashSet<User> hashSet = new HashSet<>();
        Date date = new Date();
        System.out.println("date = " + date);

        System.out.println("修改前的：");
        User user = new User();
        user.setName("zhang");
        user.setPassword("123");
        user.setBirthday(date);
        User user1 = new User();
        user1.setName("li");
        user1.setPassword("123");
        user1.setBirthday(date);


        hashSet.add(user);
        hashSet.add(user1);
        hashSet.forEach(System.out::println);
        System.out.println("修改后的：");

        user.setName("li");

        hashSet.forEach(System.out::println);
        System.out.println("--------------------------------------------");
    }


    public void testModifyInteget() {
        System.out.println("--------------------------------------------");
        HashSet<inInteger> hashSet = new HashSet<>();
        System.out.println("修改前的：");
        inInteger inInteger = new inInteger(15);
        inInteger inInteger1 = new inInteger(16);
        inInteger inInteger2 = new inInteger(16);

        hashSet.add(inInteger);
        hashSet.add(inInteger1);
        hashSet.forEach(System.out::println);
        System.out.println("hashSet = " + hashSet.add(inInteger2));
        System.out.println("修改后的：");

        inInteger.age = 16;
        System.out.println("hashSet = " + hashSet.add(new inInteger(15)));

        hashSet.forEach(System.out::println);
        System.out.println("--------------------------------------------");
    }

    public static void main(String[] args) {
        CanOrNotModifySetElements canOrNotModifySetElements = new CanOrNotModifySetElements();
        canOrNotModifySetElements.testModifyUser();
        canOrNotModifySetElements.testModifyInteget();
    }

    private class inInteger {
        int age;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            inInteger inInteger = (inInteger) o;

            return age == inInteger.age;

        }

        @Override
        public int hashCode() {
            return age;
        }

        public inInteger(int age) {
            this.age = age;
        }
    }
}
